//
//  ZYXMessageVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/10.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXMessageVC.h"

#import "ZYXComeHeaderView.h"
#import "ZYXMessageCell.h"
#import "ZYXMessageAppleCell.h"

#import "ZYXMeaasageModel.h"
#import "ZYXMsgDetailVC.h"
#import "ZYXStudyModel.h"
@interface ZYXMessageVC ()<ZYXComeHeaderViewDelegate>
@property (strong,nonatomic) NSString *typeStr;
@property (strong,nonatomic) NSMutableArray *dataArr;
///页数
@property (strong,nonatomic) NSString *page;
@end

@implementation ZYXMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息中心";

}

-(void)setupViews{
    ZYXComeHeaderView *headerView = [[ZYXComeHeaderView alloc]initWithFram:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(48)) TitleOne:@"消息通知" andTitleTwo:@"回复提醒"];
    headerView.delegate =self;
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(HEIGHT(48));
    }];
    
    self.tableView.frame = CGRectMake(0, HEIGHT(48), SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-HEIGHT(48));
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXMessageCell" bundle:nil] forCellReuseIdentifier:@"ZYXMessageCell"];
     [self.tableView registerNib:[UINib nibWithNibName:@"ZYXMessageAppleCell" bundle:nil] forCellReuseIdentifier:@"ZYXMessageAppleCell"];
    self.tableView.estimatedRowHeight = HEIGHT(51);
    
    
    self.typeStr = @"one";
    [self.view addSubview:self.tableView];
    [self setupRefresh];
    self.page = @"1";
    [self loadRequestPage:@"1"];
}
#pragma mark 加载数据
- (void)setupRefresh {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
        [self.dataArr removeAllObjects];
        [self loadRequestPage:self.page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = SF(@"%ld",[self.page integerValue] + 1);
        [self loadRequestPage:self.page];
    }];
    /// 设置contentInset 补缺
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = HomeIndicator_Height;
}
- (void)endRefresh {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
- (void)loadRequestPage:(NSString *)page {
    if ([self.typeStr isEqualToString:@"one"]) {
        NSLog(@"%@",kInterfaceAllUrl(PATH_WDNesMsgRemind));
        [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_WDNesMsgRemind) parmas:@{@"user_id":UID,@"page":page} successBloock:^(NSDictionary *responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"] isEqualToNumber:@200]) {
                
                if ([self.page integerValue] <= [responseObject[@"data"][@"last_page"] integerValue]) {
                    
                    NSArray *modelArr = [ZYXStudyInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
                    [self.dataArr addObjectsFromArray:modelArr];
                 
                    
                }else{
                    [JKToast showWithText:@"暂无加载更多内容"];
                }
                
                   [self.tableView reloadData];
            }else{
                [JKToast showWithText:responseObject[@"message"]];
            }
            [self endRefresh];
            
        } failureBlock:^(NSError *error) {
            [self endRefresh];
        }];

    }else{
        [NetWork GETWithUrlString:kInterfaceAllUrl(PATH_WDNesMsg) parmas:@{@"user_id":UID,@"page":page} successBloock:^(NSDictionary *responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"] isEqualToNumber:@200]) {
                
                if ([self.page integerValue] <= [responseObject[@"info"][@"last_page"] integerValue]) {
                
                    NSArray *modelArr = [ZYXStudyInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"info"][@"data"]];
                    [self.dataArr addObjectsFromArray:modelArr];
                  
                    
                }else{
                    [JKToast showWithText:@"暂无加载更多内容"];
                }
                
                  [self.tableView reloadData];
            }else{
                [JKToast showWithText:responseObject[@"message"]];
            }
            [self endRefresh];
            
        } failureBlock:^(NSError *error) {
            [self endRefresh];
        }];
    }
}

#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.typeStr isEqualToString:@"one"]) {
         ZYXStudyInfoModel *model = self.dataArr[indexPath.row];
        ZYXMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXMessageCell"];
          cell.model = model;
        return cell;
    }else{
        ZYXStudyInfoModel *model = self.dataArr[indexPath.row];
        ZYXMessageAppleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXMessageAppleCell"];
        cell.model = model;
        return cell;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.typeStr isEqualToString:@"one"]) {
        return HEIGHT(51);
    }else{
        return  UITableViewAutomaticDimension;
    }

}
/*-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAK(weakSelf);
    if ([self.typeStr isEqualToString:@"two"]) {
        return nil;
    }else{
        ZYXStudyInfoModel *model = self.dataArr[indexPath.row];
        // 添加一个删除按钮
        UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  
            NSLog(@"%@",@{@"user_id":UID,@"noc_id":model.uid});
            [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_WDMesageDel) parmas:@{@"user_id":UID,@"noc_id":model.uid} successBloock:^(NSDictionary *responseObject) {
                NSLog(@"%@",responseObject);
                if ([responseObject[@"code"] isEqualToNumber:@200]) {
                    [weakSelf.dataArr removeObject:model];
                    [weakSelf.tableView reloadData];
                }
                
            } failureBlock:^(NSError *error) {
                
            }];
    
            NSLog(@"点击了删除");
            
        }];
        deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"#1cca62"];
        
        // 将设置好的按钮放到数组中返回
        return @[deleteRowAction];
    }
}*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击查看更多");
    if ([self.typeStr isEqualToString:@"two"]) {
        ZYXStudyInfoModel *model = self.dataArr[indexPath.row];
        ZYXMsgDetailVC *vc = [[ZYXMsgDetailVC alloc]init];
        vc.list_id = model.uid;
        [self.navigationController pushViewController:vc animated:YES];
    }
   
}
#pragma mark ZYXComeHeaderViewDelegate
-(void)clickBtnWith:(NSString *)typeStr{
        self.typeStr = typeStr;
        [self.dataArr removeAllObjects];
        self.page = @"1";
        [self loadRequestPage:self.page];
 
}
#pragma mark 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
//        ZYXMeaasageModel *model0 = [[ZYXMeaasageModel alloc]initWitNameStr:@"管理员" content:@"哈哈哈哈哈"];
//        ZYXMeaasageModel *model1 = [[ZYXMeaasageModel alloc]initWitNameStr:@"管理员" content:@"全世界华人团结起来，早日实现中华民族的伟大复兴，我们全世界华人在世人面前才有话语权,在世界的任何地方才不被欺负！"];
//        ZYXMeaasageModel *model2 = [[ZYXMeaasageModel alloc]initWitNameStr:@"我就是我员" content:@"有道理"];
//        ZYXMeaasageModel *model3 = [[ZYXMeaasageModel alloc]initWitNameStr:@"我就是我员" content:@"你真美"];
//        ZYXMeaasageModel *model4 = [[ZYXMeaasageModel alloc]initWitNameStr:@"我就是我" content:@"哈哈哈哈哈哈哈哈哈哈哈哈哈哈"];
//        ZYXMeaasageModel *model5 = [[ZYXMeaasageModel alloc]initWitNameStr:@"我就是我" content:@"哈哈哈哈哈好好好好哈哈哦啊好好哦好好哦啊爱护哦啊好好好哦嗷嗷好哦好"];
//        ZYXMeaasageModel *model6 = [[ZYXMeaasageModel alloc]initWitNameStr:@"管理我就是我" content:@"哈哈哈哈哈"];
//        _dataArr = [NSMutableArray arrayWithObjects:model0,model1,model2,model3,model4,model5,model6, nil];
        
    }
    return _dataArr;
}

@end
